package addCompo.toggleButton
{
	
	import com.greensock.TweenMax;
	import com.greensock.data.TweenMaxVars;
	import com.greensock.easing.Quint;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	public class XMToggleButton extends MovieClip
	{
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		public var btn : MovieClip;
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var isState : Boolean;
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------	
		public function XMToggleButton()
		{
			super();
			
			isState = true;
			
			btn.addEventListener(MouseEvent.CLICK , onBtnClick );
		}
		
		public function get state (): Boolean
		{
			return isState;
		}
		
		//Array, Boolean, Color, Font Name, List, Number, Object, String
		[Inspectable(name="State",defaultValue="ON", type=List, enumeration="ON,OFF")]
		public function set State  ( str : String ):void
		{
			if( str == "ON")
			{
				btn.x = 0;
			}else if (str == "OFF")
			{
				btn.x = -64;
			}
		}
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		protected function onBtnClick(e:MouseEvent):void
		{
			var targetX : Number;
			var currentTarget : MovieClip = e.currentTarget as MovieClip;
			
			TweenMax.killTweensOf( currentTarget );
			if( currentTarget.x >= 0 ) 
			{
				//OFF
				targetX = -64;
				isState = false;
			}
			else
			{
				//ON
				targetX = 0;
				isState = true;
			}
			TweenMax.to( currentTarget , 0.5 , new TweenMaxVars()
				.prop( "x", targetX )
				.ease( Quint.easeOut )
			);
		}
	}
	
}